﻿namespace ManagerClinic.DanhMuc
{
    using System.Xml;
    using System.Runtime.InteropServices;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Modules;
    using ManagerClinic.Ultilities.Ultility;

    public partial class frmCapNhatDmDanToc : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler dmDanTocEvent;
        int State = 0;

        ThietLapMaBusiness thietLapMaBusiness = new ThietLapMaBusiness();
        DmDanTocBusiness dmDanTocBusiness = new DmDanTocBusiness();
        DmDanTocEntity dmDanTocEntity = null;

        public frmCapNhatDmDanToc(int state)
        {
            InitializeComponent();
            this.State = state;
        }

        public frmCapNhatDmDanToc(int state, DmDanTocEntity obj)
        {
            InitializeComponent();

            this.State = state;
            dmDanTocEntity = obj;

            txtMaDanToc.Text = dmDanTocEntity.MaDanToc;
            txtTenDanToc.Text = dmDanTocEntity.TenDanToc;
            txtMoTa.Text = dmDanTocEntity.MoTa;
            chkTrangThaiSuDung.Checked = dmDanTocEntity.TrangThaiSuDung;
            txtMaDanToc.Enabled = false;
            txtTenDanToc.Focus();
        }

        private void frmcapnhatdmdantoc_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (validateManager1.IsValid() == false) return;
            if (Validation.ValidSpaceTextBoxKey(txtMaDanToc, "Mã dân tộc") == false) return;

            dmDanTocEntity = new DmDanTocEntity()
            {
                MaDanToc = txtMaDanToc.Text,
                TenDanToc = txtTenDanToc.Text.Trim(),
                MoTa = txtMoTa.Text.Trim(),
                TrangThaiSuDung = (bool)chkTrangThaiSuDung.Checked
            };

            if (this.State == (int)ConstantManagerClinic.StateInsertOrUpdate.Insert)
            {
                if (dmDanTocBusiness.GetDmDanToc(txtMaDanToc.Text.Trim()) != null)
                {
                    if (Validation.ValidPrimaryKey(txtMaDanToc, "Mã dân tộc") == false) return;
                }

                dmDanTocBusiness.InsertDmDanToc(dmDanTocEntity);
                Validation.ClearControl(this);
                txtMaDanToc.Focus();
            }
            else
            {
                dmDanTocBusiness.EditDmDanToc(dmDanTocEntity);
                MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (dmDanTocEvent != null)
                dmDanTocEvent(sender, e);
        }

    }
}